package com.unity3d.player;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.example.unitylibrary.databinding.ActivityMyUnityPlayerBinding;
import com.therouter.TheRouter;
import com.therouter.router.Route;

@Route(path = "http://bdnrc.org.cn/unity", description = "Unity页面")
public class MyUnityPlayerActivity extends UnityPlayerActivity {

    private static final String TAG = "MyUnityPlayerActivity";
    private ActivityMyUnityPlayerBinding binding;

    private boolean isFirstStart = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMyUnityPlayerBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Log.v(TAG, "UnityActivity onCreate()...");
        // 将Unity界面添加到unityView中
        binding.unityView.addView(mUnityPlayer);

        // 跳转
        binding.skipButton.setOnClickListener(view -> {
            TheRouter.build("http://bdnrc.org.cn/home")
                            .navigation(this);
        });

        // 方向控制
        binding.upButton.setOnClickListener(view -> Up());
        binding.downButton.setOnClickListener(view -> Down());
        binding.leftButton.setOnClickListener(view -> Left());
        binding.rightButton.setOnClickListener(view -> Right());

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.v(TAG, "UnityActivity onRestart()...");

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.v(TAG, "UnityActivity onStart()...");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.v(TAG, "UnityActivity onPause()...");

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.v(TAG, "UnityActivity onStop()...");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.v(TAG, "UnityActivity onDestroy()...");
    }

    private void AndroidExitScan(){
        Toast.makeText(this, "辰火科技", Toast.LENGTH_SHORT).show();
    }

    private void EnterHome(){
        // 启动Home页面
        TheRouter.build("http://bdnrc.org.cn/home")
                .navigation(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            Log.v(TAG, "onKeyDown Back");
            // 创建一个意图，模仿用户按下Home键的行为
//            Intent startMain = new Intent(Intent.ACTION_MAIN);
//            startMain.addCategory(Intent.CATEGORY_HOME);
//            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            startActivity(startMain);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
    private void Up(){
        UnityPlayer.UnitySendMessage("Cube", "Up", "");
    }

    private void Down(){
        UnityPlayer.UnitySendMessage("Cube", "Down", "");
    }

    private void Left(){
        UnityPlayer.UnitySendMessage("Cube", "Left", "");
    }

    private void Right(){
        UnityPlayer.UnitySendMessage("Cube", "Right", "");
    }
}